﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Questionnaire.Data.Models
{
    public partial class QuestionnaireContext : DbContext
    {
        public virtual DbSet<Option> Option { get; set; }
        public virtual DbSet<Question> Question { get; set; }
        public virtual DbSet<Questionnaire> Questionnaire { get; set; }
        public virtual DbSet<Record> Record { get; set; }
        public virtual DbSet<Result> Result { get; set; }
        public virtual DbSet<Tourist> Tourist { get; set; }
        public virtual DbSet<User> User { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
            optionsBuilder.UseSqlServer(@"Data Source=.;Initial Catalog=Questionnaire;User ID=sa;Password=jxzxc1230");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Option>(entity =>
            {
                entity.Property(e => e.CreateTime).HasColumnType("datetime");

                entity.Property(e => e.OptionDescribe)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.HasOne(d => d.Question)
                    .WithMany(p => p.Option)
                    .HasForeignKey(d => d.QuestionId)
                    .HasConstraintName("FK_OPTION_REFERENCE_QUESTION");
            });

            modelBuilder.Entity<Question>(entity =>
            {
                entity.Property(e => e.CreateTime).HasColumnType("datetime");

                entity.Property(e => e.Describe)
                    .IsRequired()
                    .HasMaxLength(20);

                entity.HasOne(d => d.Questionnaire)
                    .WithMany(p => p.Question)
                    .HasForeignKey(d => d.QuestionnaireId)
                    .OnDelete(DeleteBehavior.Cascade)
                    .HasConstraintName("FK_QUESTION_REFERENCE_QUESTION");
            });

            modelBuilder.Entity<Questionnaire>(entity =>
            {
                entity.Property(e => e.BeginTime).HasColumnType("datetime");

                entity.Property(e => e.CreateTime).HasColumnType("datetime");

                entity.Property(e => e.Describe).HasMaxLength(150);

                entity.Property(e => e.EndTime).HasColumnType("datetime");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(40);

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Questionnaire)
                    .HasForeignKey(d => d.UserId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_Questionnaire_Questionnaire");
            });

            modelBuilder.Entity<Record>(entity =>
            {
                entity.HasKey(e => new { e.UserId, e.QuestionnaireId })
                    .HasName("PK_Record");

                entity.Property(e => e.Discribe).HasMaxLength(200);

                entity.Property(e => e.Questionnaire)
                    .IsRequired()
                    .HasMaxLength(40);

                entity.Property(e => e.Time).HasColumnType("datetime");
            });

            modelBuilder.Entity<Result>(entity =>
            {
                entity.HasKey(e => new { e.Ip, e.QuestionnaireId, e.QuestionSerial, e.OptionSerial })
                    .HasName("PK_RESULT");

                entity.Property(e => e.Ip)
                    .HasColumnName("IP")
                    .HasMaxLength(15);
            });

            modelBuilder.Entity<Tourist>(entity =>
            {
                entity.Property(e => e.CommitTime).HasColumnType("datetime");

                entity.Property(e => e.FinishTime).HasColumnType("date");

                entity.Property(e => e.Ip)
                    .IsRequired()
                    .HasColumnName("IP")
                    .HasMaxLength(15);

                entity.HasOne(d => d.Questionnaire)
                    .WithMany(p => p.Tourist)
                    .HasForeignKey(d => d.QuestionnaireId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_Tourist_Questionnaire");
            });

            modelBuilder.Entity<User>(entity =>
            {
                entity.Property(e => e.Account)
                    .IsRequired()
                    .HasMaxLength(15);

                entity.Property(e => e.CreateTime).HasColumnType("datetime");

                entity.Property(e => e.HaedPortrait)
                    .IsRequired()
                    .HasMaxLength(100);

                entity.Property(e => e.NickName).HasMaxLength(20);

                entity.Property(e => e.Password)
                    .IsRequired()
                    .HasMaxLength(15);
            });
        }
    }
}